﻿using System;
using System.Drawing;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
using System.Drawing.Printing;
using System.Windows.Controls;
using System.Printing;

namespace BarQRCodeGenerator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 返回生成的Bitmap对象
        /// </summary>
        /// <param name="barcodeContent">条码内容</param>
        /// <param name="barcodeFormat">条码格式</param>
        /// <param name="height">条码高度</param>
        /// <param name="width">宽度高度</param>
        /// <returns></returns>
        public static Bitmap CreateBarcode(string barcodeContent, BarcodeFormat barcodeFormat, int height, int width)
        {
            try
            {
                BarcodeWriter writer = new BarcodeWriter();
                EncodingOptions options = new QrCodeEncodingOptions
                {
                    DisableECI = true,
                    CharacterSet = "UTF-8",
                    Width = width,
                    Height = height
                };
                writer.Options = options;
                writer.Format = barcodeFormat;
                return writer.Write(barcodeContent);

            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>  
        /// 从Bitmap转换成BitmapSource  
        /// </summary>  
        /// <param name="bmp"></param>  
        /// <returns></returns>  
        public static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
        {
            BitmapSource returnSource;
            try
            {
                returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }
            catch(Exception ex)
            {
                returnSource = null;
            }
            return returnSource;            
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var code = this.code.Text;
            var img = CreateBarcode(code,BarcodeFormat.CODE_128, 50, 200);       
            this.image.Source = ChangeBitmapToBitmapSource(img);

            PrintVisual(this.image);
        }

        /// <summary>
        /// 打印控件
        /// </summary>
        /// <param name="element"></param>
        public void PrintVisual(FrameworkElement element)
        {
            var PrintServerName = "DESKTOP-77VT0MV";
            var PrintName = "Bullzip PDF Printer";
            var printDialog = new PrintDialog();
            SetPrintProperty(printDialog);
            var printQueue = SelectedPrintServer(PrintServerName, PrintName);
            if (printQueue != null)
            {
                printDialog.PrintQueue = printQueue;
                printDialog.PrintVisual(element, "");
            }
        }

        /// <summary>
        /// 查找并获取打印机
        /// </summary>
        /// <param name="printerServerName">服务器名字： Lee-pc</param>
        /// <param name="printerName">打印机名字：Hp laserjet m1522 mfp series pcl 6 </param>
        /// <returns></returns>
        public PrintQueue SelectedPrintServer(string printerServerName, string printerName)
        {
            try
            {
                var printers = PrinterSettings.InstalledPrinters;//获取本机上的所有打印机
                PrintServer printServer = null;

                foreach (string printer in printers)
                {
                    if (printer.Contains(printerName))
                        printServer = new PrintServer("\\\\" + printerServerName);
                }

                if (printServer == null) return null;//没有找到打印机服务器

                var printQueue = printServer.GetPrintQueue(printerName);
                return printQueue;
            }
            catch (Exception ex)
            {
                return null;//没有找到打印机
            }
        }
        /// <summary>
        /// 设置打印格式
        /// </summary>
        /// <param name="printDialog">打印文档</param>
        /// <param name="pageSize">打印纸张大小 a4</param>
        /// <param name="pageOrientation">打印方向 竖向</param>
        public void SetPrintProperty(PrintDialog printDialog, PageMediaSizeName pageSize = PageMediaSizeName.ISOA4, PageOrientation pageOrientation = PageOrientation.Portrait)
        {
            var printTicket = printDialog.PrintTicket;
            printTicket.PageMediaSize = new PageMediaSize(pageSize);//A4纸
            printTicket.PageOrientation = pageOrientation;//默认竖向打印
        }

    }
}
